//
//  WBHomeViewModel.swift
//  Weibo
//
//  Created by xu on 2017/3/24.
//  Copyright © 2017年 xuantong. All rights reserved.
//

import UIKit
import YYModel
import SDWebImage

class WBHomeViewModel: NSObject {
    
    //保存模型数组
    //    var dataArray:[WBHomeModel] = [WBHomeModel]()
    var dataArray:[WBStatusViewModel] = [WBStatusViewModel]()
    
}

//MARK: 请求数据
extension WBHomeViewModel {
    
    
    
    func getHomeData(isPullUp:(Bool),finish:@escaping (Bool,Int)->()) {
        
        // 定义两个属性
        
        var sinceId:Int64 = 0
        
        var maxId: Int64 = 0
        
        if isPullUp {
            maxId = dataArray.last?.homeModel?.id ?? 0
            if maxId > 0{
                maxId -= 1
            }

        }else{
            sinceId = dataArray.first?.homeModel?.id ?? 0
        }
        
        
        //检查本地是否有缓存方法调用
        WBHomeDAL.checkCache(since_id: sinceId, max_id: maxId) { (response) in
            
            // 判断是否为nil
            guard let resArr = response else{
                finish(false,0)
                return
            }
            //进行数据缓存
            WBHomeDAL.saveCache(res: resArr as! [[String : Any]])
            
            //这里就是取到数据,将字典转模型
            let statusesArray = NSArray.yy_modelArray(with:WBHomeModel.self, json: resArr)as! [WBHomeModel]
            
            //定义一个临时的数组
            var tempArray:[WBStatusViewModel] = [WBStatusViewModel]()
            //遍历数组
            for homeModel in statusesArray{
                // 实例化statusViewModel
                let statueViewModel = WBStatusViewModel()
                statueViewModel.homeModel = homeModel
                // 添加元素->statueViewModel
                tempArray.append(statueViewModel)
                
            }

            self.downLoadSingeImage(tempArray: tempArray, finish: finish)
            
            
            if isPullUp {
                //为数组赋值
                self.dataArray = self.dataArray + tempArray

            }else{
                self.dataArray = tempArray + self.dataArray

            }
        
            
        }
    }
    
    // 下载单张图片(原创微博 还是 转发微博)方法
    private func downLoadSingeImage(tempArray:[WBStatusViewModel],finish:@escaping (Bool,Int)->()){
        
        //实例化调度组
        let group = DispatchGroup()
        // 遍历tempArray 判断是否是一张图片
        for statusViewModel in tempArray{
            // 判断原创微博是否是一张图片
            if statusViewModel.homeModel?.pic_urls?.count == 1 {
                // 使用调度组进行加标识
                group.enter()
                // 使用SD完成图片下载
                SDWebImageManager.shared().downloadImage(with: URL(string: statusViewModel.homeModel?.pic_urls?.first?.thumbnail_pic ?? "")
                    , options: [], progress: nil, completed: { (image, error, _, _, _) in
                        
                        //图片下载完成取消调度组的标识
                           group.leave()

                })
                
                
            }
            
        }
        
        
             // 通过调度组来总的监听单张图片是否下载完成
        group.notify(queue: DispatchQueue.main){
            //告诉外界进行刷新UI
            
            finish(true,tempArray.count)
        }
        
        
        
    }
}
